﻿#pragma once

#include "libdaq/backend/base_backend.h"
#include "libdaq/backend/backend_option.h"
#include "libdaq/device/DAQ217/DAQ217.h"

extern "C" {
#include "libusb-1.0/libusb.h"
}

namespace libdaq {
namespace backend {
class LockzhinerUSBBackend : public LockzhinerBaseBackend {
public:
  LockzhinerUSBBackend(libusb_device *device,
                       const std::vector<uint8_t> &endpoints_in,
                       const std::vector<uint8_t> &endpoints_out);
  ~LockzhinerUSBBackend() override;
  LockzhinerBackendType GetBackendType() override { return LockzhinerBackendType::USBBackend; }
  bool Initialize() override;
  bool Open() override;
  bool Close() override;
  bool ReadData(uint8_t endpoint_index,
                unsigned char *data,
                const int read_size,
                int *actual_size,
                const int duration_ms) override;
  bool WriteData(uint8_t endpoint_index,
                 unsigned char *data,
                 const int write_size,
                 int *actual_size,
                 const int duration_ms) override;
private:
  std::mutex mutex_;  // 读写锁
  libusb_context *context_ = nullptr;
  libusb_device *device_ = nullptr;
  libusb_device_handle *handle_ = nullptr;
  std::vector<uint8_t> endpoints_in_ = {0x00};
  std::vector<uint8_t> endpoints_out_ = {0x00};
  int nb_ifaces_ = 0; // number of interfaces
};
} // namespace backend
} // namespace libdaq
